/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cundong.recyclerview.sample;

import com.cundong.recyclerview.HeaderAndFooterRecyclerViewAdapter;
import com.cundong.recyclerview.RecyclerViewUtils;
import com.cundong.recyclerview.ResourceTable;

import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;

import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

import com.cundong.recyclerview.sample.bean.Contact;
import com.cundong.recyclerview.sample.provider.ContactItemProvider;
import com.cundong.recyclerview.sample.slice.EndlessDirectionalLayoutAbilitySlice;

import java.util.ArrayList;

/**
 * ExampleOhosTest Class
 */
public class ExampleOhosTest {
    /**
     * testBundleName
     */
    @Test
    public void testBundleName() {
        final String actualBundleName = AbilityDelegatorRegistry.getArguments().getTestBundleName();
        assertEquals("com.cundong.recyclerview", actualBundleName);
    }

    /**
     * test Contact
     */
    @Test
    public void testContact() {
        String name = "John";
        String phoneNumber = "99999";
        String newName = "lipo";
        String newPhoneNumber = "7777";
        Contact contact = new Contact(name, phoneNumber);

        assertEquals(name, contact.getUserName());
        assertEquals(phoneNumber, contact.getTelPhone());

        contact.setUserName(newName);
        contact.setTelPhone(newPhoneNumber);
        assertEquals(newName, contact.getUserName());
        assertEquals(newPhoneNumber, contact.getTelPhone());
    }

    /**
     * testContactItemProvider
     */
    @Test
    public void testContactItemProvider() {
        ArrayList<Contact> contactData = new ArrayList<>();
        String name = "User";
        String phoneNumber = "00001";
        Contact sample = new Contact(name , phoneNumber );
        contactData.add(sample);
        for (int index = 0; index < 10; index++) {
            contactData.add(new Contact("User " + index, "100000000" + index));
        }

        ContactItemProvider contactItemProvider = new ContactItemProvider(new EndlessDirectionalLayoutAbilitySlice(),
                contactData);

        assertEquals(11, contactItemProvider.getCount());
        assertEquals(sample, contactItemProvider.getItem(0));
        assertEquals(1, contactItemProvider.getItemId(1));

        ArrayList<Contact> newContactData = new ArrayList<>();
        for (int index = 0; index < 4; index++) {
            contactData.add(new Contact("User " + index, "100000000" + index));
        }
        contactItemProvider.addData(newContactData);
        assertEquals(15, contactItemProvider.getCount());
    }

    /**
     * testSetAdapter
     */
    @Test
    public void testSetAdapter() {
        ArrayList<Contact> contactData = new ArrayList<>();
        for (int index = 0; index < 20; index++) {
            contactData.add(new Contact("User " + index, "100000000" + index));
        }
        ContactItemProvider contactItemProvider = new ContactItemProvider(new MainAbility(), contactData);
        HeaderAndFooterRecyclerViewAdapter mHeaderAndFooterRecyclerViewAdapter =
                new HeaderAndFooterRecyclerViewAdapter(contactItemProvider);
        assertEquals(contactItemProvider, mHeaderAndFooterRecyclerViewAdapter.getInnerAdapter());
    }

    /**
     * test Contact
     */
    @Test
    public void testAddHeaderView() {
        ArrayList<Contact> contactData = new ArrayList<>();
        for (int index = 0; index < 20; index++) {
            contactData.add(new Contact("User " + index, "100000000" + index));
        }
        ContactItemProvider contactItemProvider = new ContactItemProvider(new MainAbility(), contactData);
        HeaderAndFooterRecyclerViewAdapter mHeaderAndFooterRecyclerViewAdapter =
                new HeaderAndFooterRecyclerViewAdapter(contactItemProvider);
        ListContainer listContainer = new ListContainer(new MainAbility());
        listContainer.setItemProvider(mHeaderAndFooterRecyclerViewAdapter);
        LayoutScatter mLayoutScatter = LayoutScatter.getInstance(new MainAbility());

        assertEquals(false, mHeaderAndFooterRecyclerViewAdapter.isHeader(0));
        assertEquals(0, mHeaderAndFooterRecyclerViewAdapter.getHeaderViewsCount());
        RecyclerViewUtils.setHeaderView(listContainer,
                mLayoutScatter.parse(ResourceTable.Layout_header_layout, null, false));

        assertEquals(true, mHeaderAndFooterRecyclerViewAdapter.isHeader(0));
        assertEquals(1, mHeaderAndFooterRecyclerViewAdapter.getHeaderViewsCount());
    }

    /**
     * testAddFooterView
     */
    @Test
    public void testAddFooterView() {
        ArrayList<Contact> contactData = new ArrayList<>();
        for (int index = 0; index < 20; index++) {
            contactData.add(new Contact("User " + index, "100000000" + index));
        }
        ContactItemProvider contactItemProvider = new ContactItemProvider(new MainAbility(), contactData);
        HeaderAndFooterRecyclerViewAdapter mHeaderAndFooterRecyclerViewAdapter =
                new HeaderAndFooterRecyclerViewAdapter(contactItemProvider);
        ListContainer listContainer = new ListContainer(new MainAbility());
        listContainer.setItemProvider(mHeaderAndFooterRecyclerViewAdapter);
        LayoutScatter mLayoutScatter = LayoutScatter.getInstance(new MainAbility());

        assertEquals(0, mHeaderAndFooterRecyclerViewAdapter.getFooterViewsCount());
        assertEquals(false, mHeaderAndFooterRecyclerViewAdapter.isFooter(20));

        RecyclerViewUtils.setFooterView(listContainer,
                mLayoutScatter.parse(ResourceTable.Layout_footer_layout, null, false));

        assertEquals(true, mHeaderAndFooterRecyclerViewAdapter.isFooter(20));

        RecyclerViewUtils.setFooterView(listContainer,
                mLayoutScatter.parse(ResourceTable.Layout_footer_layout, null, false));
        RecyclerViewUtils.setFooterView(listContainer,
                mLayoutScatter.parse(ResourceTable.Layout_footer_layout, null, false));

        assertEquals(3, mHeaderAndFooterRecyclerViewAdapter.getFooterViewsCount());

        mHeaderAndFooterRecyclerViewAdapter.removeAllFooterView();

        assertEquals(0, mHeaderAndFooterRecyclerViewAdapter.getFooterViewsCount());
    }
}